package com.kgc.app.service.impl;

import com.kgc.app.dto.AppVersionDto;
import com.kgc.app.mapper.AppInfoMapper;
import com.kgc.app.mapper.AppVersionMapper;
import com.kgc.app.mapper.DataDictionaryMapper;
import com.kgc.app.pojo.*;
import com.kgc.app.service.AppVersionService;
import com.sun.org.apache.regexp.internal.RE;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class AppVersionServiceImpl implements AppVersionService {

    //引入AppVersion的mapper接口对象
    @Autowired
    private AppVersionMapper appVersionMapper;

    //引入AppInfo的mapper接口对象
    @Autowired
    private AppInfoMapper appInfoMapper;

    //引入DataDictionary的mapper接口对象
    @Autowired
    private DataDictionaryMapper dataDictionaryMapper;

    @Transactional(readOnly = true)
    @Override
    public List<AppVersionDto> selectAppVersionByAppId(Long appId) {
        List<AppVersionDto> appVersionDtos = new ArrayList<AppVersionDto>();
        /**
         * 1.先查询app_info表 主键查询
         *   如果app_info查询出来没有 versionId，就直接返回null
         * 2.查询app_version表，以appid作为查询条件  查询出来一个或多个
         * 3.遍历查询的app版本信息，然后根据publishStatus去数据字典表中查询对应的发布状态
         */
        AppInfo appInfo = appInfoMapper.selectByPrimaryKey(appId);

        if (null == appInfo.getVersionId()){
            return null;
        }

        //查询历史版本
        //创建条件类
        AppVersionExample appVersionExample = new AppVersionExample();
        AppVersionExample.Criteria criteria = appVersionExample.createCriteria();

        //拼接条件
        criteria.andAppIdEqualTo(appId);

        //执行查询
        List<AppVersion> appVersions = appVersionMapper.selectByExample(appVersionExample);

        //遍历
        for (AppVersion appVersion : appVersions) {
            //查询对应的发布状态
            DataDictionaryExample dataDictionaryExample = new DataDictionaryExample();
            DataDictionaryExample.Criteria criteria1 = dataDictionaryExample.createCriteria();

            //拼接条件
            criteria1.andTypeNameEqualTo("发布状态");
            criteria1.andValueIdEqualTo(appVersion.getPublishStatus());

            List<DataDictionary> dataDictionaries = dataDictionaryMapper.selectByExample(dataDictionaryExample);

            //存储查询到的数据
            AppVersionDto appVersionDto = new AppVersionDto();

            //使用beanUtils工具
            //注册时间处理
            ConvertUtils.register(new DateLocaleConverter(), Date.class);

            try {
                BeanUtils.copyProperties(appVersionDto,appVersion);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            //设置软件名称
            appVersionDto.setSoftwareName(appInfo.getSoftwareName());

            //设置发布状态名称
            appVersionDto.setPublishName(dataDictionaries.get(0).getValueName());

            //把dto存储到集合中
            appVersionDtos.add(appVersionDto);

        }

        return appVersionDtos;
    }

    @Transactional
    @Override
    public boolean addAppVersion(AppVersion appVersion) {
        //实现步骤

        //先添加app的版本信息，并且得到版本id,返回的主键在appVersion
        int row = appVersionMapper.insertSelective(appVersion);

        //对app信息进行修改，修改versionId的值即可
        //根据主键修改
        AppInfo appInfo = new AppInfo();
        //设置app_info的主键
        appInfo.setId(appVersion.getAppId());
        //设置app_info的版本id
        appInfo.setVersionId(appVersion.getId());

        //调用app_info根据主键修改的方法,用于修改app信息中的版本信息
        appInfoMapper.updateByPrimaryKeySelective(appInfo);

        return row > 0;
    }

    @Override
    public AppVersion selectAppVersionById(Long id) {
        return appVersionMapper.selectByPrimaryKey(id);
    }
}
